<template>
  <FilterHeader @search="handleSearch"></FilterHeader>
  <div class="container">
    <h1>报告库</h1>
    <div class="tabs">
      <div v-for="item in tabs" :key="item.id" :class="['tab', { active: queryParms.category_id === item.id }]"
        @click="selectTab(item.id)">
        {{ item.name }}
        <span class="underline"></span>
      </div>
    </div>
    <div class="list">
      <CommonCard @edit="edit" @preview="preview" v-for="item in templateList" :item="item" :key="item.id" 
        role="teacher"></CommonCard>
      <el-empty v-if="!templateList.length" :image-size="100" description="暂无数据"
        style="margin: 0 auto; margin-top: 100px;" />
    </div>
    <div class="pagination">
      <el-pagination background layout="prev, pager, next" :total="total" hide-on-single-page />
    </div>
    <el-dialog destroy-on-close modal-class="global_dialog" v-model="dialogVisible" title="编辑" width="531"
      @close="handleClose">
      <!-- 默认插槽，用于内容 -->
      <slot>
        <el-form ref="formRef" :rules="rules" :model="form" label-width="auto" style="max-width: 600px;">
          <el-form-item label="报告名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item prop="memo" label="备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注">
            <el-input v-model="form.memo" />
          </el-form-item>
        </el-form>
      </slot>
      <!-- 自定义按钮插槽 -->
      <template #footer>
        <slot name="footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleConfirm">保存</el-button>
        </slot>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="jsx">
import { onMounted, ref, onActivated, onDeactivated, reactive } from "vue";
import FilterHeader from "@/components/FilterHeader.vue";
import CommonCard from "@/components/CommonCard.vue";
import request from "@/request";
import { useRouter } from "vue-router";
import { ElMessage, genFileId } from "element-plus";

const router = useRouter();
const tabs = ref([]);
const total = ref(1);
const templateList = ref([]);
const dialogVisible = ref(false);
const queryParms = ref({
  category_id: "",
  keywords: "",
  page: 1,
  limit: 10,
});
const curId = ref(null);
const formRef = ref(null);
const form = ref({
  name: "",
  memo: "",
});
const rules = ref({
  name: [{ required: true, message: "请输入报告名称", trigger: "blur" }],
});
onActivated(() => {
  // 调用时机为首次挂载
  console.log("我被激活了");
  // 以及每次从缓存中被重新插入时
});

onDeactivated(() => {
  // 在从 DOM 上移除、进入缓存
  console.log("我被移除了");
  // 以及组件卸载时调用
});
onMounted(async () => {
  console.log("我被挂载了");
  await getCategoryTableList();
  getMyTemplateList();
});
const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      // 提交表单逻辑，例如发请求
      try {
        const formData = new FormData();
        formData.append('id', curId.value);
        formData.append('row[name]', form.value.name);
        formData.append('row[memo]', form.value.memo);
        const res = await request.post("/api/master/template/edit", formData, {
          headers: { 'Content-Type': 'multipart/form-data' }
        });
        console.log(res);
        
        if (res.code == 1) {
          console.log('ss');
          
          ElMessage({
            type: "success",
            message: res.msg || "编辑成功",
          });
          dialogVisible.value = false;
          getMyTemplateList();
        }
        console.log("提交表单数据：", form);

      } catch (error) { }


    } else {
      return false;
    }
  });
};
const handleClose = () => {
  dialogVisible.value = false;
  formRef.value.resetFields();
};
const edit = (data) => {
  dialogVisible.value = true;
  curId.value = data.id;
  form.value.name = data.name;
  form.value.memo = data.memo;
};

const preview = (data) => {
  // ?templateId=1&reportId=2
  router.push({
    path: "reportDetail",
    query: {
      templateId: data.id,
      name: data.name,
      mode: "preview",
    },
  });
};
// 获取分类列表
const getCategoryTableList = async () => {
  const res = await request.get("/api/master/from/category");
  if (res.code == 1) {
    tabs.value = res.data;
    queryParms.value.category_id = res.data[0].id;
  }
};
// 获取我的模板列表
const getMyTemplateList = async () => {
  const res = await request.post("/api/master/template/list", queryParms.value);
  if (res.code == 1) {
    templateList.value = res.rows;
    total.value = res.total;
  }
};
// 搜索
const handleSearch = (keyword) => {
  queryParms.value.keywords = keyword;
  getMyTemplateList();
};
// 切换tab
const selectTab = (id) => {
  queryParms.value.category_id = id;
  getMyTemplateList();
};
</script>

<style lang="less" scoped>
.container {
  // background: linear-gradient(135deg, #E6E8FA 0%,  #E0EDF8 100%);
  // background-color: rgb(236, 238, 240);
  background-color: #f1f1fa;
  height: calc(100vh - 80px);
  padding: 0 40px;
  overflow: hidden;
}

h1 {
  font-weight: bold;
  font-size: 20px;
  color: #333;
  margin: 46px 0 30px 0;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-top: 20px;
  border-bottom: solid #deddf1 1px;
  padding-bottom: 22px;
}

.tab {
  cursor: pointer;
  font-size: 16px;
  color: #333;
  position: relative;
  transition: all 0.3s ease;

  &:hover {
    color: #2d53f8;
  }

  &.active {
    color: #2d53f8;
    font-weight: bold;
    font-size: 18px;
    box-sizing: border-box;
    // animation: textGlow 1s infinite alternate;
  }
}

.list {
  display: flex;
  flex-wrap: wrap;
  margin-top: 20px;
  gap: 20px;
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

@keyframes textGlow {
  0% {
    text-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
  }

  50% {
    text-shadow: 0 0 15px rgba(0, 123, 255, 0.6);
  }

  100% {
    text-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
  }
}

/* 悬浮渐变背景动画 */
@keyframes cardHover {
  0% {
    background: linear-gradient(135deg, #ffffff, #f0f4ff);
  }

  50% {
    background: linear-gradient(135deg, #f0f4ff, #e6e8fa);
  }

  100% {
    background: linear-gradient(135deg, #e6e8fa, #ffffff);
  }
}

/* 粒子特效 */
.tab.active::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 10px;
  height: 10px;
  background: rgba(0, 123, 255, 0.5);
  border-radius: 50%;
  animation: particle 0.8s ease-out forwards;
}

@keyframes particle {
  0% {
    transform: scale(0);
    opacity: 1;
  }

  100% {
    transform: scale(3);
    opacity: 0;
  }
}
</style>
